home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / opml.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  6.8 KB  |  179 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import os
  5. from xml.dom import minidom
  6. from xml.sax import saxutils
  7. from xml.parsers import expat
  8. from datetime import datetime
  9. from StringIO import StringIO
  10. import app
  11. import util
  12. import feed
  13. import views
  14. import prefs
  15. import config
  16. import folder
  17. import dialogs
  18. import eventloop
  19. from gtcache import gettext as _
  20. from gtcache import ngettext
  21.  
  22. class Exporter(object):
  23.     
  24.     def __init__(self):
  25.         self.io = StringIO()
  26.         self.currentFolder = None
  27.  
  28.     
  29.     def exportSubscriptions(self):
  30.         
  31.         callback = lambda p: self.exportSubscriptionsTo(p)
  32.         title = _('Export OPML File')
  33.         app.delegate.askForSavePathname(title, callback, None, u'miro_subscriptions.opml')
  34.  
  35.     
  36.     def exportSubscriptionsTo(self, pathname):
  37.         now = datetime.now()
  38.         self.io.write(u'<?xml version="1.0" encoding="utf-8" ?>\n')
  39.         self.io.write(u'<!-- OPML generated by Miro v%s on %s -->\n' % (config.get(prefs.APP_VERSION), now.ctime()))
  40.         self.io.write(u'<opml version="2.0">\n')
  41.         self.io.write(u'\t<head>\n')
  42.         self.io.write(u'\t\t<title>%s</title>\n' % os.path.basename(pathname))
  43.         self.io.write(u'\t\t<dateCreated>%s</dateCreated>\n' % now.ctime())
  44.         self.io.write(u'\t\t<docs>http://www.opml.org/spec2</docs>\n')
  45.         self.io.write(u'\t</head>\n')
  46.         self.io.write(u'\t<body>\n')
  47.         tabOrder = util.getSingletonDDBObject(views.channelTabOrder)
  48.         for tab in tabOrder.getAllTabs():
  49.             if tab.isChannelFolder():
  50.                 self._openFolderEntry(tab.obj)
  51.                 continue
  52.             if tab.isFeed():
  53.                 self._writeFeedEntry(tab.obj)
  54.                 continue
  55.         
  56.         if self.currentFolder is not None:
  57.             self._closeFolderEntry()
  58.         
  59.         self.io.write(u'\t</body>\n')
  60.         self.io.write(u'</opml>\n')
  61.         f = open(pathname, 'w')
  62.         f.write(self.io.getvalue().encode('utf-8'))
  63.         f.close()
  64.  
  65.     exportSubscriptionsTo = eventloop.asIdle(exportSubscriptionsTo)
  66.     
  67.     def _openFolderEntry(self, folder):
  68.         if self.currentFolder is not None:
  69.             self._closeFolderEntry()
  70.         
  71.         self.currentFolder = folder
  72.         self.io.write(u'\t\t<outline text=%s>\n' % saxutils.quoteattr(folder.getTitle()))
  73.  
  74.     
  75.     def _closeFolderEntry(self):
  76.         self.io.write(u'\t\t</outline>\n')
  77.  
  78.     
  79.     def _writeFeedEntry(self, thefeed):
  80.         if self.currentFolder is not None and thefeed.getFolder() is None:
  81.             self._closeFolderEntry()
  82.             self.currentFolder = None
  83.         
  84.         if self.currentFolder is None:
  85.             spacer = u'\t\t'
  86.         else:
  87.             spacer = u'\t\t\t'
  88.         if isinstance(thefeed.getActualFeed(), feed.RSSFeedImpl):
  89.             feedtype = u'type="rss"'
  90.         else:
  91.             feedtype = u'type="mirofeed"'
  92.         self.io.write(u'%s<outline %s text=%s xmlUrl=%s />\n' % (spacer, feedtype, saxutils.quoteattr(thefeed.getTitle()), saxutils.quoteattr(thefeed.getURL())))
  93.  
  94.  
  95.  
  96. class Importer(object):
  97.     
  98.     def __init__(self):
  99.         self.currentFolder = None
  100.         self.ignoredFeeds = 0
  101.         self.importedFeeds = 0
  102.  
  103.     
  104.     def importSubscriptions(self):
  105.         
  106.         callback = lambda p: self.importSubscriptionsFrom(p)
  107.         title = _('Import OPML File')
  108.         app.delegate.askForOpenPathname(title, callback, None, _('OPML Files'), [
  109.             'opml'])
  110.  
  111.     
  112.     def importSubscriptionsFrom(self, pathname, showSummary = True):
  113.         f = open(pathname, 'r')
  114.         content = f.read()
  115.         f.close()
  116.         
  117.         try:
  118.             dom = minidom.parseString(content)
  119.             root = dom.documentElement
  120.             body = root.getElementsByTagName('body').pop()
  121.             self._walkOutline(body)
  122.             dom.unlink()
  123.             if showSummary:
  124.                 self.showImportSummary()
  125.         except expat.ExpatError:
  126.             self.showXMLError()
  127.  
  128.  
  129.     importSubscriptionsFrom = eventloop.asIdle(importSubscriptionsFrom)
  130.     
  131.     def showXMLError(self):
  132.         title = _(u'OPML Import failed')
  133.         message = _(u'The selected OPML file appears to be invalid. Import was interrupted.')
  134.         dialog = dialogs.MessageBoxDialog(title, message)
  135.         dialog.run()
  136.  
  137.     
  138.     def showImportSummary(self):
  139.         title = _(u'OPML Import summary')
  140.         message = ngettext(u'Successfully imported %d feed.', u'Successfully imported %d feeds.', self.importedFeeds) % self.importedFeeds
  141.         if self.ignoredFeeds > 0:
  142.             message += '\n'
  143.             message += ngettext(u'Skipped %d feed already present.', u'Skipped %d feeds already present.', self.ignoredFeeds) % self.ignoredFeeds
  144.         
  145.         dialog = dialogs.MessageBoxDialog(title, message)
  146.         dialog.run()
  147.  
  148.     
  149.     def _walkOutline(self, node):
  150.         
  151.         try:
  152.             children = node.childNodes
  153.             for child in children:
  154.                 if hasattr(child, 'getAttribute'):
  155.                     if child.hasAttribute('xmlUrl'):
  156.                         self._handleFeedEntry(child)
  157.                     else:
  158.                         self._handlerFolderEntry(child)
  159.                 child.hasAttribute('xmlUrl')
  160.             
  161.             self.currentFolder = None
  162.         except Exception:
  163.             e = None
  164.             print e
  165.  
  166.  
  167.     
  168.     def _handleFeedEntry(self, entry):
  169.         url = entry.getAttribute('xmlUrl')
  170.         f = feed.getFeedByURL(url)
  171.  
  172.     
  173.     def _handlerFolderEntry(self, entry):
  174.         title = entry.getAttribute('text')
  175.         self.currentFolder = folder.ChannelFolder(title)
  176.         self._walkOutline(entry)
  177.  
  178.  
  179.